<?php
class Authcode{
	public function __construct(){
		@header("Content-type: image/PNG");  
		$x_size=70; //验证码图片的宽度
		$y_size=20; //验证码图片的高度
		$len = 4;
		$ccnum=$this->RandomString($len); //随机生成的字符串
		session_start();
		$_SESSION['login_check_number'] = strtolower($ccnum);
		
		$ccimg = imagecreatetruecolor($x_size,$y_size);
		$white = imagecolorallocate($ccimg,255,255,255); //第一次调用设置背景色
		$black = imagecolorallocate($ccimg,0,0,0); //边框颜色
		imagefilledrectangle($ccimg,0,0,$x_size,$y_size,$white); //画一矩形填充
		$noise_color = imagecolorallocate($ccimg, 235, 235, 235);

		$sz = ($x_size*$y_size)/4;
		for( $i=0; $i<$sz; $i++ ) {
			imagefilledellipse($ccimg, mt_rand(0,$x_size), mt_rand(0,$y_size), 1, 1, $noise_color);
		}

		imagerectangle($ccimg,0,0,$x_size-1,$y_size-1,$black); //画一矩形框
		//将验证码写入图案
		for($i = 0;$i < $len;$i++){
		   $x = 10 + $i * ($x_size - 15)/$len;
		   $y = mt_rand(1,5);
		   $color = imagecolorallocate($ccimg,mt_rand(0,225),mt_rand(0,150),mt_rand(0,225));
		   imagechar($ccimg,5,$x,$y,substr($ccnum,$i,1),$color);
		}
		imagepng($ccimg);
		imagedestroy($ccimg);
		exit();
	}
	
	private function RandomString($len) {
		static $salt = "abchefghj23kmnpqrstuvwxyz45ABCEFGH67JKMNPQRST89UVWXYZ";
		$rand = substr(str_shuffle($salt),0,$len);
		return $rand;
	}
}